package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.TemperatureAndHumidity;
import com.siwei.mes.mapper.TemperatureAndHumidityMapper;
import com.siwei.mes.service.TemperatureAndHumidityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 温湿度管理(TemperatureAndHumidity)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:21
 */
@Service
public class TemperatureAndHumidityServiceImpl implements TemperatureAndHumidityService {
    @Resource
    private TemperatureAndHumidityMapper temperatureAndHumidityMapper;

    @Override
    public Long create(TemperatureAndHumidity entity) {
        entity.init(true);
        temperatureAndHumidityMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(TemperatureAndHumidity entity) {
        TemperatureAndHumidity temperatureAndHumidity = temperatureAndHumidityMapper.selectByPrimaryKey(entity.getId());
        assertTrue(temperatureAndHumidity == null, "温湿度管理信息不存在");
        entity.init(false);
        temperatureAndHumidityMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        TemperatureAndHumidity temperatureAndHumidity = temperatureAndHumidityMapper.selectByPrimaryKey(id);
        assertTrue(temperatureAndHumidity == null, "温湿度管理信息不存在");
        temperatureAndHumidityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<TemperatureAndHumidity> getPage(PageQuery<TemperatureAndHumidity, TemperatureAndHumidity> query) {
        TemperatureAndHumidity searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> temperatureAndHumidityMapper.getList(searchParam));
    }

    @Override
    public TemperatureAndHumidity loadById(Long id) {
        return temperatureAndHumidityMapper.loadById(id);
    }

    @Override
    public List<TemperatureAndHumidity> getAll(TemperatureAndHumidity entity) {
        return temperatureAndHumidityMapper.getList(entity);
    }

    @Override
    public TemperatureAndHumidity getLastInfo(TemperatureAndHumidity entity) {
        return temperatureAndHumidityMapper.getLastInfo(entity);
    }
}
